'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 掠夺
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var taskService = require('./taskService');
var battleRecordModel = require('../models/battleRecordModel');
var PlayerManager = require('../libs/playerManager');
var constantUtil = require('../util/constantUtil');

exports.getRecordList = function (role_id, callback) {
    var battle_record;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.getModel(['role', 'accrued'], cb);
            },
            function (cb) {
                battleRecordModel.getRecordListByDefRoleId(role_id, cb);
            },
            function (record_list, cb) {
                var last_logout_time = player_manager.role.getLastLogoutTime();
                var relive_piont_per = constantUtil.value('relive_piont_per');
                var crown = 0;
                var earn_diamond = 0;
                var lost_gold = 0;
                var def_num = 0;
                var battle_record_list = [];
                _.each(record_list, function (record_item) {
                    if (record_item.create_time >= last_logout_time) {
                        var progress = 100 - record_item.getProgress();
                        if (progress < 20) {
                            crown += 1;
                        } else if (progress < 35) {
                            crown += 2;
                        } else if (progress < 50) {
                            crown += 3;
                        } else if (progress < 75) {
                            crown += 4;
                        } else if (progress < 90) {
                            crown += 5;
                        } else {
                            crown += 6;
                        }
                        def_num++;
                        earn_diamond +=
                            parseInt(record_item.getReliveDiamond() * relive_piont_per / 100);
                        lost_gold += record_item.getEarnGold();
                    }
                    var battle_record_item = {
                        role_id: record_item.getAtkRoleId(),
                        name: record_item.getAtkName(),
                        lv: record_item.getAtkLv(),
                        job_type: record_item.getAtkJobType(),
                        soldier_list: record_item.getAtkSoldierList(),
                        skill_list: record_item.getAtkSkillList(),
                        lost_gold: record_item.getEarnGold(),
                        lost_food: record_item.getEarnFood(),
                        earn_diamond: record_item.getReliveDiamond() * relive_piont_per / 100,
                        star: record_item.getStar(),
                        create_time: record_item.getCreateTime()
                    };
                    var star = record_item.getStar();
                    if (star === 0) {
                        battle_record_item.lost_cup = record_item.getCup();
                    } else {
                        battle_record_item.lost_cup = -record_item.getCup();
                    }
                    battle_record_list.push(battle_record_item);
                });
                battle_record = {
                    battle_record_list: battle_record_list
                };
                if (def_num > 0) {
                    battle_record.total_record = {
                        crown: Math.ceil(crown / def_num),
                        earn_diamond: earn_diamond,
                        lost_gold: lost_gold,
                        def_num: def_num
                    };
                }
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
            }
            callback(null, battle_record);
        }
    );
};

/**
 * 进入掠夺
 */
exports.enterBattle = function (player_manager) {
    taskService.addBattleNum(player_manager);
};

/**
 * 离开战斗
 * @param  {[type]} player_manager [description]
 * @return {[type]}                [description]
 */
exports.exitBattle = function (atk_player_manager, def_player_manager, gold, star, cup) {
    if (star > 0) {
        taskService.addBattleWinNum(atk_player_manager);
        taskService.addBattleEarnTotalGold(atk_player_manager, gold);
        taskService.addBattleEarnGoldOneTime(atk_player_manager, gold);
    } else {
        taskService.addBattleDefWinNum(def_player_manager);
    }
    taskService.addCupNum(atk_player_manager, atk_player_manager.battle.getCup());
};
